/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.dao.FinishingDAOInterface;
import com.wii.elpro.dao.hb.FinishingDAOImpl;
import com.wii.elpro.entity.Finishing;
import com.wii.elpro.entity.User;
import com.wii.elpro.web.form.FinishingNewForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author Nicholai
 */
public class FinishingSaveAction extends Action {
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        String forward;
        FinishingNewForm f = (FinishingNewForm) form;
        FinishingDAOInterface dao = new FinishingDAOImpl();
        Finishing fns = new Finishing();
        fns.setKeterangan(f.getKeterangan());
        fns.setNama(f.getNama());
        ActionMessages messages = new ActionMessages();        
        String fw = request.getParameter("submit");
        try {
            dao.save(fns);
            messages.add("customMessage", new ActionMessage("customMessage", "Your data is successfully saved."));
            saveMessages(sess, messages);
            forward = fw;
        } catch (Exception ex) {
            messages.add("message", new ActionMessage("customMessage", "Application is fail to save your data. Please contact my developer."));
            saveMessages(sess, messages);
            ex.printStackTrace();
            forward = fw;
        }
        return mapping.findForward(forward);
    }
}
